package com.example.app.interceptor;

import com.example.app.model.User;
import com.example.app.utils.JwtHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by wangtao on 2018/3/5.
 */
public class UserInterceptor extends HandlerInterceptorAdapter {

    @Value("${jwt.user.tokenHeader}")
    private String tokenHeader;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader(tokenHeader);
        User user = jwtHelper.getUserByToken(token);
        if(user == null){
            throw new Exception("access_token不合法");
        }
        return super.preHandle(request, response, handler);
    }
}
